/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("cw_djlc_wljgwh",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.Finance.IServices;
using VOL.Core.DBManager;
using VOL.Core.Utilities;
using System.Linq;
using VOL.Core.Filters;

namespace VOL.Finance.Controllers
{
    public partial class cw_djlc_wljgwhController
    {
        private readonly Icw_djlc_wljgwhService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public cw_djlc_wljgwhController(
            Icw_djlc_wljgwhService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }
        WebResponseContent webResponse = new WebResponseContent();
        /**
         * 批量添加定价物料价格维护UUID关系表数据
         */
        [HttpPost, Route("batchInsertWljgwh")]
        [ApiActionPermission()]
        public ActionResult batchInsertWljgwh(string djlcid)
        {
            using (var db = DBServerProvider.DbContext)
            {
                Guid djlcId = Guid.Parse(djlcid);
                //status 0启用，1不启用
                List<cw_djlc_wlzb> list = db.Set<cw_djlc_wlzb>().Where(x => x.cw_djlc_djzb_id == djlcId).ToList();
                foreach (var item in list)
                {
                    Guid wlzbId = item.cw_djlc_wlzb_id;
                    var codename = item.codename;
                    
                    cw_djlc_jgwh jgwh = db.Set<cw_djlc_jgwh>().Where(x => x.codename == codename && x.status == 0).ToList().FirstOrDefault();
                    string jgwhId = jgwh != null ? jgwh.cw_djlc_jgwh_id.ToString() : "";
                    cw_djlc_wlzb wlzb = new cw_djlc_wlzb();
                    wlzb.cw_djlc_djzb_id = djlcId;
                    wlzb.cw_djlc_wlzb_id = wlzbId;
                    wlzb.cw_djlc_jgwh_id = Guid.Parse(jgwhId);
                    db.Add(wlzb);
                }
                db.SaveChanges();
            }
            webResponse.Code = "-1";
            return Json(webResponse.OK("保存成功！"));

        }
        /// <summary>
        /// 同步价格的时候需要维护 cw_djlc_wljgwh 物料价格维护关联表 
        /// 
        /// </summary>
        /// <param name="cw_djlc_djzb_id"></param>
        /// <returns></returns>
        [HttpPost, Route("batchUpdateOfJgwh")]
        [ApiActionPermission()]
        public ActionResult batchUpdateOfJgwh(string cw_djlc_djzb_id)
        {
            using (var db = DBServerProvider.DbContext)
            {
                Guid djlcId = Guid.Parse(cw_djlc_djzb_id);
                //status 0启用，1不启用
                List<cw_djlc_wljgwh> list = db.Set<cw_djlc_wljgwh>().Where(x => x.cw_djlc_djzb_id == djlcId).ToList();
                var dbcw_djlc_wljgwh = db.Set<cw_djlc_wljgwh>();
                var dbcw_djlc_wlzb = db.Set<cw_djlc_wlzb>();
                var dbcw_djlc_jgwh = db.Set<cw_djlc_jgwh>();
                foreach (var item in list)
                {
                    Guid wlzbId = (Guid)item.cw_djlc_wlzb_id;
                    cw_djlc_wlzb wlzb = dbcw_djlc_wlzb.Where(x => x.cw_djlc_wlzb_id == wlzbId).ToList().FirstOrDefault();
                    var pricestart = wlzb.priceeffectivedatestart;
                    var priceend = wlzb.priceeffectivedateend;
                    string codename = "";
                    if (wlzb!=null)
                    {
                        codename = wlzb.codename;
                    }
                    //cw_djlc_jgwh jgwh = dbcw_djlc_jgwh.Where(x => x.codename == codename && x.status == 0 && (x.effectivedatestart != null && wlzb.priceeffectivedatestart !=null && x.effectivedatestart.Value.Year == wlzb.priceeffectivedatestart.Value.Year) && (x.effectivedateend != null && wlzb.priceeffectivedateend != null && x.effectivedateend.Value.Year == wlzb.priceeffectivedateend.Value.Year)).ToList().FirstOrDefault();
                    cw_djlc_jgwh jgwh = dbcw_djlc_jgwh.Where(x => x.codename == codename && x.status == 0).ToList().FirstOrDefault();
                    if (jgwh!=null)
                    {
                        if (jgwh.effectivedatestart != null && wlzb.priceeffectivedatestart != null && jgwh.effectivedatestart.Value.Year == wlzb.priceeffectivedatestart.Value.Year && jgwh.effectivedateend != null && wlzb.priceeffectivedateend != null && jgwh.effectivedateend.Value.Year == wlzb.priceeffectivedateend.Value.Year)
                        {
                            item.cw_djlc_jgwh_id = jgwh.cw_djlc_jgwh_id;
                        }
                    }
                    db.Update(item);
                }
                db.SaveChanges();
            };
            return Json(webResponse.OK("更新成功！"));
        }
    }
}
